#include<bits/stdc++.h>
using namespace std;
pair<int,int>a[500006];
int n;
map<int,int>mp[500005],mp2[5000005];
vector<int>vec[500005];
queue<int>q;
long long ans,siz[500005];
void dfs(int u){
	siz[u]=1;
	for(int i=0;i<vec[u].size();i++){
		int v=vec[u][i];
		if(siz[v])continue;
		dfs(v);
		siz[u]+=siz[v];
		ans+=1ll*siz[v]*(n-siz[v]);
	}
}
int dis[4005];
bool vis[4005];
int main(){
freopen("game.in","r",stdin);
freopen("game.out","w",stdout);
	cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i].first>>a[i].second;
	for(int i=1;i<=n;i++){
		mp[a[i].first][a[i].second]=i;
	//	mp2[a[i].second][a[i].first]=i;
	}
	int cnt=0;
	for(int i=1;i<=n;i++){
		int id=mp[a[i].first][a[i].second+1];
		int id2=mp[a[i].first+1][a[i].second];
		if(id!=0){
		cnt++;
			vec[id].push_back(i);
			vec[i].push_back(id);
		}	
		if(id2!=0){
		cnt++;
			vec[id2].push_back(i);
			vec[i].push_back(id2);
		}
	}
	if(n<=4000){
		for(int i=1;i<=n;i++){
			memset(vis,0,sizeof(vis));
			//q.clear();
			vis[i]=1;dis[i]=0;q.push(i);
			while(!q.empty()){
				int u=q.front();q.pop();
				for(int i=0;i<vec[u].size();i++){
					int v=vec[u][i];
					if(vis[v])continue;
					dis[v]=dis[u]+1;vis[v]=1;
					q.push(v);
				}
			}
			for(int j=1;j<=n;j++)ans+=dis[j];
		}
		cout<<ans/2<<"\n";
		return 0;
	}
	if(cnt==n-1){
		//cout<<"this is a tree\n";
		dfs(1);
		cout<<ans<<'\n';
		return 0;
	}
	sort(a+1,a+n+1,[](pair<int,int> a,pair<int,int> b){return a.first<b.first;});
	for(int i=1;i<=n;i++){
		ans+=1ll*(a[i].first)*(i-(n-i+1));
	}
	sort(a+1,a+n+1,[](pair<int,int> a,pair<int,int> b){return a.second<b.second;});
	for(int i=1;i<=n;i++){
		ans+=1ll*(a[i].second)*(i-(n-i+1));
	}
	cout<<ans<<'\n';
}

